<?php 
/**
 * 微信公众号（服务号）
 */
namespace Weixin\Controller;
use Think\Controller;
vendor('Weixin.src.WeixinFuwu');
class WeixinFuwuController extends \WeixinFuwu{
	/**
	 * 初始化
	 * Enter description here ...
	 */
	public function __construct(){
        parent::__construct();

		//动态获取公众号信息
        $this->appid = C('APPID');
        $this->appsecret = C('APP_SECRET');
        $this->token = C('TOKEN');
        $this->account = $info['account'];
        $this->original_id = $info['accountpwd'];
        $this->appurl = $info['url'];
        $this->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].U('Weixin/WeixinFuwu/callback');
	}


    /**
     * 回调地址
     * @param string $success_url 成功后跳转地址
     * @param string $state 附加参数
     */
    function callback($success_url='',$state='')
    {
        $uInfo = $this->oauth2_user_info($_GET['code']);
        $openid = $_SESSION['openid'] = $uInfo['openid'];
        $_SESSION['uInfo'] = $uInfo;

        //判断用户是否存在
        $User = M('user');
        $userid = $User -> where("openid= '%s'",$uInfo['openid'])->getField('pk_id');

        if (empty($userid)) {
            //基本授权需自行获取用户信息
//            if ($uInfo['scope'] == 'snsapi_base') {
                $Weixin = new \Weixin\Controller\WeixinFuwuController();
                $uInfo = $Weixin->getUserInfo($openid);

                if($state){
                    //拦截未关注
                    if(!$uInfo['subscribe']){
                        //显示关注二维码

                        die(header('location:' . U('Weixin/Remind/index')));

                    }
                }


//                dump($uInfo);die;

                //未关注
//                if ($uInfo['subscribe'] == 0) {
//                    //未关注,跳转至关注页面
//                    die(header('location:' . U('Weixin/Remind/index').'?way='.$_GET['state']));
//
//                }
                //添加用户
                $data['openid'] = $uInfo['openid'];

//                $userid = M('user')->add($data);
                $User = M('user')->add($data);
//                $userid = $User -> where("openid= '%s'",$uInfo['openid'])->getField('pk_id');

//            }
        }

        $_SESSION['userid'] = $userid;
        $_SESSION['openid'] = $uInfo['openid'];

        if ($success_url) {

            header('location:' . $success_url);

        } else {
            //默认跳转

        }
    }


    /**
     * 微信入口
     */
    function wx_run(){
        $this->run();
    }


    /**
     * 用户关注时
     */
    protected function onSubscribe(){
        $eventkey = $this->request['eventkey'];

//        writeToFile($this->request,'./Public/',date('YmdHis').'getdata.txt','a+b');

        if(is_string($eventkey) && $eventkey){
            $this->onScan(); //扫描带参数二维码关注
        }else{
            $this->responseText('欢迎关注！');
        }
    }


    protected function onScan()
    {

        //获取情景值
        $eventkey = $this->request['eventkey'];
        if (!is_numeric($eventkey)) {
            $eventkey = str_replace('qrscene_', '', $eventkey);
        }

        //获取用户信息
        $joinopenid = $this->request['fromusername'];//被邀请者


        $userModel = M('user');//用户表
        $joinuserid = $userModel->where("openid= '%s'",$joinopenid)->getField('pk_id');
        if (empty($joinuserid)) {
            $data['openid'] = $joinopenid;
            $data['create_time'] = date('Y-m-d H:i:s');
            $userModel->add($data);
            $joinuserid = $userModel->where("openid= '%s'",$joinopenid)->getField('pk_id');

        }
        $_SESSION['joinuserid'] = $joinuserid;
        $_SESSION['joinopenid'] = $joinopenid;

        //将数据写入invite邀请表
        $invite = M('invite'); //邀请表
        $data['join_id'] = $joinuserid; //被邀请人ID
        $data['join_openid'] = $joinopenid;//被邀请人微信openID
        $data['invite_id'] = $eventkey;//邀请人ID
        $data['create_time'] = date('Y-m-d H:i:s');
        $invite ->add($data);

    }


}

